#pragma once

#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QList>
#include "material.h"
#include "operation.h"

namespace MISystem
{

    class SqlHelper
    {
    public:
        SqlHelper();
        ~SqlHelper();

        bool addMaterial(const MaterialStock& material);
        bool updateMaterial(const MaterialStock& material);
        bool deleteMaterial(const QString& materialId);
        MaterialStock getMaterialById(const QString& materialId);
        QList<MaterialStock> getAllMaterials();
        bool materialExists(const QString& materialId);

        bool addStockOperation(const StockOperation& operation);
        QList<StockOperation> getOperationsByMaterialId(const QString& materialId);
        QList<StockOperation> getAllOperations();
        QList<StockOperation> getOperationsByDateRange(const QDateTime& start, const QDateTime& end);

        bool stockIn(const QString& materialId, int quantity, const QString& operatorName);
        bool stockOut(const QString& materialId, int quantity, const QString& operatorName);

        QString getLastError() const;

    private:
        bool initDatabase();
        void closeDatabase();

        bool createTables();
        bool updateMaterialQuantity(const QString& materialId, int quantityChange);

    private:
		QString _dbPath = "misystem.db";
        QSqlDatabase _db;
        QString _lastError;
    };

}